package calabashandroidplugin.popup.actions;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.util.Scanner;

import static java.nio.file.StandardCopyOption.*;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.part.FileEditorInput;
import org.osgi.framework.Bundle;

import calabashandroidplugin.utils.Executor;
import calabashandroidplugin.utils.PluginUtils;

public class GenerateStructure implements IObjectActionDelegate {

	private Shell shell;

	/**
	 * Constructor for Action1.
	 */
	public GenerateStructure() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		Bundle bundle = Platform.getBundle("CalabashAndroidPlugin");
		URL fileURL = bundle.getEntry("src/calabashandroidplugin/resources/gen.bat");

//		//System.out.println(fileURL.);
//		File file = null;
//		try {
//		    file = new File(FileLocator.resolve(fileURL).toURI());
//		} catch (URISyntaxException e1) {
//		    e1.printStackTrace();
//		} catch (IOException e1) {
//		    e1.printStackTrace();
//		}
//		
//		System.out.println(file.exists());
//		try {
//			System.out.println(file.getCanonicalPath());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String fullPath = getFullPath();
//
//		Executor exec = new Executor();
//		
//		Scanner s = null;
//		try {
//			s = exec.execute(file.getCanonicalPath(), fullPath);
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		MessageConsole myConsole = findConsole("Calabash Android");
//		MessageConsoleStream out = myConsole.newMessageStream();
//		out.println("-> Generating folder structure");
//		out.println(s.next());
////		out.println("Full path of actual project : " + fullPath);
////		out.println(s.next());
//////		out.print("Resource Location : " + resourceLocation);
//////		out.print("Workspace Location : " + myWorkspaceRoot.toString());
////
//		exec.closeScanner();
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	private MessageConsole findConsole(String name) {
		ConsolePlugin plugin = ConsolePlugin.getDefault();
		IConsoleManager conMan = plugin.getConsoleManager();
		IConsole[] existing = conMan.getConsoles();
		for (int i = 0; i < existing.length; i++)
			if (name.equals(existing[i].getName()))
				return (MessageConsole) existing[i];
		// no console found, so create a new one
		MessageConsole myConsole = new MessageConsole(name, null);
		conMan.addConsoles(new IConsole[] { myConsole });
		return myConsole;
	}

	
	public String getProjectName() {
		IEditorPart editorPart = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		String activeProjectName = null;

		if (editorPart != null) {
			IFileEditorInput input = (FileEditorInput) editorPart
					.getEditorInput();
			IFile file = input.getFile();
			IProject activeProject = file.getProject();
			activeProjectName = activeProject.getName();
		}
		return activeProjectName;
	}

	public String getWorkspacePath() {
		IPath configPath = ResourcesPlugin.getWorkspace().getRoot()
				.getLocation();
		String workspacePath = configPath.toString();
		return workspacePath;
	}

	public String getFullPath() {
		return getWorkspacePath() + "/" + getProjectName();

	}

}
